package org.wangle.auth.config.auth;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.wangle.api.user.pojo.User;
import org.wangle.api.user.provider.UserProvider;

import javax.annotation.Resource;

@Service("userDetailService")
public class MyUserDetailService implements UserDetailsService {


    @Resource
    private UserProvider userProvider;

    // 重写loadUserByUsername（实现用户认证逻辑）
    @Override
    public UserDetails loadUserByUsername(String mobile) throws UsernameNotFoundException {
        User user = userProvider.getBaseMapper().selectOne(new QueryWrapper<User>().lambda().eq(User::getPhone, mobile));
        if(ObjectUtils.isEmpty(user)){
            throw new RuntimeException("没有该用户");
        }
        SpringSecurityUser springSecurityUser = new SpringSecurityUser();
        springSecurityUser.setPassword(user.getPassword());
        springSecurityUser.setUsername(mobile);
        return springSecurityUser;
    }

}